<script setup>
import { Delete, Edit, Search, Refresh } from "@element-plus/icons-vue";
import { ref } from "vue";
import ChannelSelect from "@/views/article/components/ChannelSelect.vue";
import { artGetListService, artDelService } from "@/api/other/article";
import { formatTime } from "@/utils/format";
import ArticleEdit from "./components/ArticleEdit.vue";
import Pagination from "@/components/Pagination.vue";

// 列表数据
const articleList = ref([]);
const total = ref(0);
const loading = ref(false);

// 搜索参数
const params = ref({
  pagenum: 1,
  pagesize: 5,
  cate_id: "",
  state: "",
});

// 获取数据
const getArticleList = async () => {
  loading.value = true;
  const { data } = await artGetListService(params.value);
  articleList.value = data.data;
  total.value = data.total;
  loading.value = false;
};
getArticleList();

/**
 * 删除文章
 * @param {Object} row - 文章对象，包含文章信息，至少包含文章标题和ID
 * @returns {Promise<void>} 不返回任何内容
 */
const onDeleteArticle = async (row) => {
  // 打印待删除文章的信息
  console.log(row);

  // 显示删除确认框
  await ElMessageBox.confirm(
    `你确认删除“${row.title}”文章信息吗？`,
    "温馨提示",
    {
      type: "warning",
      confirmButtonText: "确认",
      cancelButtonText: "取消",
    }
  );

  // 调用文章删除服务
  await artDelService(row.id);

  // 显示删除成功的消息
  ElMessage({ type: "success", message: "删除成功" });

  // 重新获取文章列表
  getArticleList();
};

// 获得抽屉ref
const articleEditRef = ref();

// 添加文章
const onAddArticle = () => {
  articleEditRef.value.open({});
};

// 编辑
const onEditArticle = (row) => {
  articleEditRef.value.open(row);
};

// 添加修改成功
const onSuccess = (type) => {
  if (type === "add") {
    // 如果是添加，需要跳转渲染最后一页，编辑直接渲染当前页
    const lastPage = Math.ceil((total.value + 1) / params.value.pagesize);
    params.value.pagenum = lastPage;
  }
  getArticleList();
};

// 搜索
const onSearch = () => {
  params.value.pagenum = 1;
  getArticleList();
};

// 重置搜索
const onReset = () => {
  params.value.pagenum = 1;
  params.value.cate_id = "";
  params.value.state = "";
  getArticleList();
};

// 分页：改变每页显示的页数
const onSizeChange = (size) => {
  params.value.pagenum = 1;
  params.value.pagesize = size;
  getArticleList();
};
/**
 * 当当前页发生变化时的处理函数
 * 主要用于更新请求参数中的页码，并重新获取文章列表。
 *
 * @param {number} page - 新的当前页码
 */
const onCurrentChange = (page) => {
  // 更新参数中的页码
  params.value.pagenum = page;
  // 调用获取文章列表的函数
  getArticleList();
};
</script>
<template>
  <pageContainer title="文章管理">
    <template #extra>
      <el-button type="primary" @click="onAddArticle()">添加文章</el-button>
    </template>
    <el-form inline style="width: 100%">
      <el-form-item label="文章分类：">
        <ChannelSelect width="180px" v-model="params.cate_id"></ChannelSelect>
      </el-form-item>
      <el-form-item label="发布状态：">
        <el-select style="width: 180px" clearable v-model="params.state">
          <el-option label="已发布" value="已发布"></el-option>
          <el-option label="草稿" value="草稿"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" :icon="Search" @click="onSearch"
          >搜索</el-button
        >
        <el-button :icon="Refresh" @click="onReset()">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="articleList" style="width: 100%" v-loading="loading">
      <el-table-column label="文章标题" width="400">
        <template #default="{ row }">
          <el-link type="primary" :underline="false">{{ row.title }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="分类" prop="cate_name"></el-table-column>
      <el-table-column label="发表时间" prop="pub_date">
        <template #default="{ row }">
          {{ formatTime(row.pub_date) }}
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="state"></el-table-column>
      <el-table-column label="操作" width="100">
        <template #default="{ row }">
          <el-button
            :icon="Edit"
            circle
            plain
            type="primary"
            @click="onEditArticle(row)"
          ></el-button>
          <el-button
            :icon="Delete"
            circle
            plain
            type="danger"
            @click="onDeleteArticle(row)"
          ></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>

    <!-- 使用分页组件 -->
    <Pagination
      :currentPage="params.pagenum"
      :pageSize="params.pagesize"
      :total="total"
      :pageSizes="[5, 10]"
      @size-change="onSizeChange"
      @current-change="onCurrentChange"
    />

    <!-- 抽屉 -->
    <ArticleEdit ref="articleEditRef" @success="onSuccess"></ArticleEdit>
  </pageContainer>
</template>
<style lang="scss" scoped></style>
